package com.cmos.um.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmos.um.beans.CtiMessage;
import com.cmos.um.iservice.IAcdSV;
import com.cmos.um.iservice.ICtiMessageSV;
import com.cmos.um.utils.Result;
import com.cmos.um.utils.ResultUtil;
import com.cmos.um.utils.WSContent;
import com.cmos.um.utils.WSRtnJsonCtx;
import com.cmos.um.utils.exception.ExceptionEnum;
import com.cmos.um.utils.http.RestClientHttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class AcdSVImpl implements IAcdSV {
    private final static Logger logger = LoggerFactory.getLogger(AcdSVImpl.class);
    @Autowired
    private ICtiMessageSV ctiMessageservice;

    @Autowired
    private RestClientHttpRequest clien;

    /**
     * 向所有cti新增分机接口
     *
     * @param params
     * @return
     */
    @Override
    public Result addAcd(JSONObject params) {
        List<CtiMessage> listCtiMessage = ctiMessageservice.queryAll();
        JSONArray sendParams = new JSONArray();
        JSONObject sendParam = new JSONObject();
        JSONObject Jparams = params.getJSONObject("params");
        ExceptionEnum rtnMsg = ExceptionEnum.OK;
        for (CtiMessage ctiInfo : listCtiMessage) {
            String acdIp = ctiInfo.getAcdServerIp();
            sendParam.put("telephoneNumber", Jparams.getString("telephoneNumber"));
            logger.info("向" + acdIp + "acd查询分机");
            WSRtnJsonCtx listResult = clien.doPost(WSContent.extension, acdIp, "findExtension", WSContent._OTHER, sendParam);
            sendParam.put("ctiId", ctiInfo.getCtiId());
            sendParam.put("passwd", Jparams.getString("passwd"));
            sendParams.add(sendParam);
            //如果查到此分机号则直接返回
            if (listResult.getContextObj() != null) {
                rtnMsg = ExceptionEnum.ACD_ADDER;
                continue;
            } else {
                //如果没查到数据则新增
                logger.info("向" + acdIp + "acd添加分机");
                WSRtnJsonCtx result = clien.doPost(WSContent.extension, acdIp, "operateExtension", WSContent._SAVE, Jparams);
                if (!"0".equals(result.getRescode())) {
                    logger.error("调用新增分机接口失败，错误信息为：" + result.getResJsonObj().toJSONString());
                    return ResultUtil.error(ExceptionEnum.CTI_ERROR);
                }
            }

        }
        return ResultUtil.success(sendParams, rtnMsg);
    }

    /**
     * 向所有cti删除分机接口
     *
     * @param params
     * @return
     */
    @Override
    public Result deleteAcd(JSONObject params) {
        List<CtiMessage> listCtiMessage = ctiMessageservice.queryAll();
        JSONObject Jparams = params.getJSONObject("params");
        for (CtiMessage ctiInfo : listCtiMessage) {
            String acdIp = ctiInfo.getAcdServerIp();
            logger.info("向" + acdIp + "cti删除分机");
            WSRtnJsonCtx result = clien.doPost(WSContent.extension, acdIp, "operateExtension", WSContent._DELETE, Jparams);
            if (!"0".equals(result.getRescode())  ) {
                logger.error("调用删除分机接口失败，错误信息为：" + result.getResJsonObj().toJSONString());
                return ResultUtil.error(ExceptionEnum.CTI_ERROR);
            }
        }
        return ResultUtil.success(ExceptionEnum.OK);
    }

}
